package com.asset.salesforce.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.model.Employee;
import com.asset.salesforce.model.EmployeeData;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.AppointmentService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CallService;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CompanyService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.LeadService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Controller
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private CountryService countryService;
	@Autowired
	private CityService cityService;
	@Autowired
	private CallService callService;
	@Autowired
	private CalendarService calendarService;
	@Autowired
	private AppointmentService appointmentService;
	@Autowired
	private LeadService leadService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private CompanyService companyService;
	 
//	@RequestMapping(value="/getEmployeeData/{email}/{password}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
//	 @ResponseBody
//	 public SalesData getEmployeeData(@PathVariable String email,@PathVariable String password){
//   	System.out.println("connection successful><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//	    Employee employee = new Employee();
//	    employee=employeeService.getEmployeeByEmailAndPassword(email, password);
//	    if(employee!=null){
//	    	if(employee.getCompany()!=null)employee.setCompanyId(employee.getCompany().getId());
//			 if(employee.getPhoto()!=null)employee.setPhotoId(employee.getPhoto().getId());
//			 if(employee.getCountry()!=null)employee.setCountryId(employee.getCountry().getId());
//			 if(employee.getJobTitle()!=null)employee.setJobTitleId(employee.getJobTitle().getId());
//			 if(employee.getDept()!=null)employee.setDeptId(employee.getDept().getId());
//			 if(employee.getManager()!=null)employee.setManagerId(employee.getManager().getId());
//	    	 
//			 SalesData data = new SalesData();
//			 List<Country>countries = countryService.getAll();
//			 List<City>cities = cityService.getAllCities();
//			 Company company = companyService.getCompanyById(new Company(), employee.getCompany().getId());
//			 List<Company>companies = new ArrayList<Company>();
//			 List<Lead>leads = leadService.getLeads(employee.getId());
//		    	for (Lead lead : leads) {
//		    		if(lead.getLeadOwner()!=null)lead.setLeadOwnerId(lead.getLeadOwner().getId());
//					if(lead.getCity()!=null)lead.setCityId(lead.getCity().getId());
//					if(lead.getCountry()!=null)lead.setCountryId(lead.getCountry().getId());
//				}
//		    	 List<Account>accounts = accountService.getAllAccountByEmpId(employee.getId());
//			 	    for (Account account : accounts) {
//			 	    	if(account.getAccountCreator()!=null)account.setAccountCreatorId(account.getAccountCreator().getId());
//						if(account.getCity()!=null)account.setCityId(account.getCity().getId());
//						if(account.getCountry()!=null)account.setCountryId(account.getCountry().getId());
//					}
//			 	   List<Contact>contacts = contactService.getAllContactByEmpId(employee.getId());
//			 	    for (Contact contact : contacts) {
//			 	    	if(contact.getCreator()!=null)contact.setCreatorId(contact.getCreator().getId());
//					}
//			 	   List<Appointment>appointments = appointmentService.getAppointmentEventsByEmployeeIdAndDate(employee.getId());
//			 	    for (Appointment appointment : appointments) {
//			 	    	if(appointment.getSalesMan()!=null)appointment.setSalesManId(appointment.getSalesMan().getId());
//						if(appointment.getContactedPerson()!=null)appointment.setContactedPersonId(appointment.getContactedPerson().getId());
//					}
//			 	   List<Call>calls = callService.getCallEventsByEmployeeIdAndDate(employee.getId());
//			 	    for (Call call : calls) {
//			 	    	if(call.getCrmStaff()!=null)call.setCrmStaffId(call.getCrmStaff().getId());
//						if(call.getContactedPerson()!=null)call.setContactedPersonId(call.getContactedPerson().getId());
//					}
//			 	   List<Calendar>calendars = calendarService.getEventsbyEmployeeId(employee.getId());
//			 	    for (Calendar calendar : calendars) {
//			 	    	if(calendar.getEmpId()!=null)calendar.setEmployeeId(calendar.getEmpId().getId());
//					}
//			 	   List<Employee>employees = companyService.getActivatedEmployeesByCompanyName(company.getCompanyName());
//					 for (Employee employee2 : employees) {
//						 if(employee2.getCompany()!=null)
//							 employee2.setCompanyId(employee2.getCompany().getId());
//			 			 if(employee2.getPhoto()!=null)employee2.setPhotoId(employee2.getPhoto().getId());
//			 			 if(employee2.getCountry()!=null)employee2.setCountryId(employee2.getCountry().getId());
//			 			 if(employee2.getJobTitle()!=null)employee2.setJobTitleId(employee2.getJobTitle().getId());
//			 			 if(employee2.getDept()!=null)employee2.setDeptId(employee2.getDept().getId());
//			 			 if(employee2.getManager()!=null)employee2.setManagerId(employee2.getManager().getId());
//					 }
//			 if(countries.size()>0){
//				 data.setCountries(countries);
//			 }
//	    	 if(cities.size()>0){
//	    		 data.setCities(cities);
//	    	 }
//	    	 if(company!=null){
//	    		 companies.add(company);
//		    	 data.setCompanies(companies);
//	    	 }
//	    	 if(employees.size()>0){
//	    		 data.setEmployees(employees);
//	    	 }
//	    	 if(leads.size()>0){
//	    		 data.setLeads(leads);
//	    	 }
//	    	 if(accounts.size()>0){
//	    		 data.setAccounts(accounts); 
//	    	 }
//	    	 if(contacts.size()>0){
//	    		 data.setContacts(contacts);
//	    	 }
//	    	 if(appointments.size()>0){
//	    		 data.setAppointments(appointments);
//	    	 }
//	    	 if(calls.size()>0){
//	    		 data.setCalls(calls);
//	    	 }
//	    	 if(calendars.size()>0){
//	    		 data.setCalendars(calendars);
//	    	 }
//	    	return data;
//	    }
//	 return null;
//	}
	@RequestMapping(value="/getEmployeeData/{email}/{password}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	 @ResponseBody
	 public EmployeeData getLogin(@PathVariable String email,@PathVariable String password){
		EmployeeData employeeData=null;
		try {
			System.out.println("connection successful><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			 Employee employee = new Employee();
			    employee=employeeService.getEmployeeByEmailAndPassword(email, password);
			    if(employee!=null){
			    	employeeData = new EmployeeData();
			    	employeeData.setUser("true");
			    	employeeData.setEmployeeId(employee.getId().toString());
			    }
			    else{
			    	 employeeData=null;
			    }
		} catch (Exception e) {
			// TODO: handle exception
		}
	 return employeeData;
	}
	 @RequestMapping(value="/getEmployee", method = RequestMethod.GET,produces="application/json")
	 @ResponseBody
	 public String getEmployee(){
		 Employee employee = employeeService.getEmployeeByEmailAndPassword("hisham@owner.com", "clever21");
		 
		 if(employee.getCompany()!=null)
			 employee.setCompanyId(employee.getCompany().getId());
		 if(employee.getPhoto()!=null)employee.setPhotoId(employee.getPhoto().getId());
		 if(employee.getCountry()!=null)employee.setCountryId(employee.getCountry().getId());
		 if(employee.getJobTitle()!=null)employee.setJobTitleId(employee.getJobTitle().getId());
		 if(employee.getDept()!=null)employee.setDeptId(employee.getDept().getId());
		 if(employee.getManager()!=null)employee.setManagerId(employee.getManager().getId());
		 
		 
		 String json =null;
		 try {
			 ObjectMapper objectMapper = new ObjectMapper();  
			 objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
			 json = objectMapper.writeValueAsString(employee);
			System.out.println(json);  
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		 return json;
	 }
}
